home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / views / MyPrintJob.as < prev    next >
Encoding:
Text File  |  2008-05-21  |  11.0 KB  |  352 lines

  1. package views
  2. {
  3.    import flash.accessibility.*;
  4.    import flash.debugger.*;
  5.    import flash.display.*;
  6.    import flash.errors.*;
  7.    import flash.events.*;
  8.    import flash.external.*;
  9.    import flash.filters.*;
  10.    import flash.geom.*;
  11.    import flash.media.*;
  12.    import flash.net.*;
  13.    import flash.printing.*;
  14.    import flash.profiler.*;
  15.    import flash.system.*;
  16.    import flash.text.*;
  17.    import flash.ui.*;
  18.    import flash.utils.*;
  19.    import flash.xml.*;
  20.    import mx.binding.*;
  21.    import mx.containers.Canvas;
  22.    import mx.controls.Image;
  23.    import mx.core.UIComponentDescriptor;
  24.    import mx.core.mx_internal;
  25.    import mx.events.PropertyChangeEvent;
  26.    import mx.managers.PopUpManager;
  27.    import mx.styles.*;
  28.    
  29.    public class MyPrintJob extends Canvas
  30.    {
  31.       private static var _watcherSetupUtil:IWatcherSetupUtil;
  32.       
  33.       private var _bindings:Array;
  34.       
  35.       public var _bindingsByDestination:Object;
  36.       
  37.       [Bindable]
  38.       private var _143903767printedImg:Image;
  39.       
  40.       [Bindable]
  41.       private var _2138617268whichPage:String = "";
  42.       
  43.       [Bindable]
  44.       private var _1796000984twoPages:Boolean = false;
  45.       
  46.       public var _bindingsBeginWithWord:Object;
  47.       
  48.       [Bindable]
  49.       private var _859611628imageURL:String;
  50.       
  51.       private var _watchers:Array;
  52.       
  53.       private var _documentDescriptor_:UIComponentDescriptor;
  54.       
  55.       public function MyPrintJob()
  56.       {
  57.          _documentDescriptor_ = new UIComponentDescriptor({
  58.             "type":Canvas,
  59.             "propertiesFactory":function():Object
  60.             {
  61.                return {"childDescriptors":[new UIComponentDescriptor({
  62.                   "type":Image,
  63.                   "id":"printedImg",
  64.                   "events":{"init":"__printedImg_init"}
  65.                })]};
  66.             }
  67.          });
  68.          _1796000984twoPages = false;
  69.          _2138617268whichPage = "";
  70.          super();
  71.          mx_internal::_document = this;
  72.       }
  73.       
  74.       public static function set watcherSetupUtil(param1:IWatcherSetupUtil) : void
  75.       {
  76.          MyPrintJob._watcherSetupUtil = param1;
  77.       }
  78.       
  79.       [Bindable(event="propertyChange")]
  80.       public function get printedImg() : Image
  81.       {
  82.          return this._143903767printedImg;
  83.       }
  84.       
  85.       public function set printedImg(param1:Image) : void
  86.       {
  87.          var _loc2_:Object = null;
  88.          _loc2_ = this._143903767printedImg;
  89.          if(_loc2_ !== param1)
  90.          {
  91.             this._143903767printedImg = param1;
  92.             dispatchEvent(PropertyChangeEvent.createUpdateEvent(this,"printedImg",_loc2_,param1));
  93.          }
  94.       }
  95.       
  96.       public function set imageURL(param1:String) : void
  97.       {
  98.          var _loc2_:Object = null;
  99.          _loc2_ = this._859611628imageURL;
  100.          if(_loc2_ !== param1)
  101.          {
  102.             this._859611628imageURL = param1;
  103.             dispatchEvent(PropertyChangeEvent.createUpdateEvent(this,"imageURL",_loc2_,param1));
  104.          }
  105.       }
  106.       
  107.       public function __printedImg_init(param1:Event) : void
  108.       {
  109.          imageInit(param1);
  110.       }
  111.       
  112.       public function set whichPage(param1:String) : void
  113.       {
  114.          var _loc2_:Object = null;
  115.          _loc2_ = this._2138617268whichPage;
  116.          if(_loc2_ !== param1)
  117.          {
  118.             this._2138617268whichPage = param1;
  119.             dispatchEvent(PropertyChangeEvent.createUpdateEvent(this,"whichPage",_loc2_,param1));
  120.          }
  121.       }
  122.       
  123.       private function _MyPrintJob_bindingsSetup() : void
  124.       {
  125.          var binding:Binding = null;
  126.          if(!_bindings)
  127.          {
  128.             _bindings = [];
  129.          }
  130.          if(!_watchers)
  131.          {
  132.             _watchers = [];
  133.          }
  134.          binding = new Binding(this,function():Object
  135.          {
  136.             return "images/large/" + imageURL;
  137.          },function(param1:Object):void
  138.          {
  139.             printedImg.source = param1;
  140.          },"printedImg.source");
  141.          _bindings[0] = binding;
  142.       }
  143.       
  144.       [Bindable(event="propertyChange")]
  145.       public function get twoPages() : Boolean
  146.       {
  147.          return this._1796000984twoPages;
  148.       }
  149.       
  150.       private function imageInit(param1:Event) : void
  151.       {
  152.          twoPages = param1.target.contentWidth > param1.target.contentHeight ? true : false;
  153.          param1.target.width = param1.target.contentWidth;
  154.          param1.target.height = param1.target.contentHeight;
  155.          param1.target.x = (param1.target.width - this.stage.width) / 2;
  156.          param1.target.y = -(param1.target.height - this.stage.height) / 2;
  157.          trace("final width " + param1.target.width + " height " + param1.target.height);
  158.       }
  159.       
  160.       public function printPages() : void
  161.       {
  162.          if(twoPages)
  163.          {
  164.             printSelection();
  165.             return;
  166.          }
  167.          doPrinting();
  168.       }
  169.       
  170.       private function printSelection() : void
  171.       {
  172.          var _loc1_:PrintSelection = null;
  173.          _loc1_ = PopUpManager.createPopUp(this,PrintSelection,true) as PrintSelection;
  174.          _loc1_.x = (this.parent.width - _loc1_.width) / 2;
  175.          _loc1_.y = (this.parent.height - _loc1_.height) / 2;
  176.          _loc1_.oCreator = this;
  177.          _loc1_.imageURL = this.imageURL;
  178.       }
  179.       
  180.       public function set twoPages(param1:Boolean) : void
  181.       {
  182.          var _loc2_:Object = null;
  183.          _loc2_ = this._1796000984twoPages;
  184.          if(_loc2_ !== param1)
  185.          {
  186.             this._1796000984twoPages = param1;
  187.             dispatchEvent(PropertyChangeEvent.createUpdateEvent(this,"twoPages",_loc2_,param1));
  188.          }
  189.       }
  190.       
  191.       private function _MyPrintJob_bindingExprs() : void
  192.       {
  193.          var _loc1_:* = undefined;
  194.          _loc1_ = "images/large/" + imageURL;
  195.       }
  196.       
  197.       [Bindable(event="propertyChange")]
  198.       public function get imageURL() : String
  199.       {
  200.          return this._859611628imageURL;
  201.       }
  202.       
  203.       [Bindable(event="propertyChange")]
  204.       public function get whichPage() : String
  205.       {
  206.          return this._2138617268whichPage;
  207.       }
  208.       
  209.       override public function initialize() : void
  210.       {
  211.          var target:MyPrintJob = null;
  212.          var watcherSetupUtilClass:Object = null;
  213.          mx_internal::setDocumentDescriptor(_documentDescriptor_);
  214.          _MyPrintJob_bindingsSetup();
  215.          target = this;
  216.          if(_watcherSetupUtil == null)
  217.          {
  218.             watcherSetupUtilClass = getDefinitionByName("_views_MyPrintJobWatcherSetupUtil");
  219.             watcherSetupUtilClass["init"](null);
  220.          }
  221.          _watcherSetupUtil.setup(this,function(param1:String):*
  222.          {
  223.             return target[param1];
  224.          },_bindings,_watchers);
  225.          super.initialize();
  226.       }
  227.       
  228.       public function doPrinting() : void
  229.       {
  230.          var pj:PrintJob = null;
  231.          var pagesToPrint:int = 0;
  232.          var options:PrintJobOptions = null;
  233.          var origWidth:int = 0;
  234.          var origHeight:int = 0;
  235.          var origRotation:Number = NaN;
  236.          var rect:Rectangle = null;
  237.          var pScaleX:Number = NaN;
  238.          var pScaleY:Number = NaN;
  239.          pj = new PrintJob();
  240.          pagesToPrint = 0;
  241.          options = new PrintJobOptions();
  242.          options.printAsBitmap = true;
  243.          origWidth = printedImg.width;
  244.          origHeight = printedImg.height;
  245.          origRotation = printedImg.rotation;
  246.          rect = new Rectangle(0,0,origWidth,origHeight);
  247.          if(pj.start())
  248.          {
  249.             if(PrintJobOrientation.LANDSCAPE == pj.orientation)
  250.             {
  251.                printedImg.rotation = -90;
  252.             }
  253.             trace("PrintJob " + pj.pageWidth + ", " + pj.pageHeight + " orientation " + pj.orientation);
  254.             if(true == twoPages)
  255.             {
  256.                if(PrintJobOrientation.LANDSCAPE == pj.orientation)
  257.                {
  258.                   pScaleX = pj.pageWidth / origHeight;
  259.                   pScaleY = pj.pageHeight / (origWidth / 2);
  260.                }
  261.                else
  262.                {
  263.                   pScaleX = pj.pageWidth / (origWidth / 2);
  264.                   pScaleY = pj.pageHeight / origHeight;
  265.                }
  266.             }
  267.             else if(PrintJobOrientation.LANDSCAPE == pj.orientation)
  268.             {
  269.                pScaleX = pj.pageWidth / origHeight;
  270.                pScaleY = pj.pageHeight / origWidth;
  271.             }
  272.             else
  273.             {
  274.                pScaleX = pj.pageWidth / origWidth;
  275.                pScaleY = pj.pageHeight / origHeight;
  276.             }
  277.             trace("pScaleX " + pScaleX + " pScaleY " + pScaleY);
  278.             if(PrintJobOrientation.LANDSCAPE == pj.orientation)
  279.             {
  280.                if(pScaleX > pScaleY)
  281.                {
  282.                   printedImg.scaleX = pScaleX;
  283.                   printedImg.scaleY = pScaleX;
  284.                }
  285.                else
  286.                {
  287.                   printedImg.scaleX = pScaleY;
  288.                   printedImg.scaleY = pScaleY;
  289.                }
  290.             }
  291.             else if(pScaleX < pScaleY)
  292.             {
  293.                printedImg.scaleX = pScaleX;
  294.                printedImg.scaleY = pScaleX;
  295.             }
  296.             else
  297.             {
  298.                printedImg.scaleX = pScaleY;
  299.                printedImg.scaleY = pScaleY;
  300.             }
  301.             printedImg.validateNow();
  302.             trace("newWidth " + printedImg.width + " newHeight " + printedImg.height);
  303.             try
  304.             {
  305.                if(twoPages)
  306.                {
  307.                   if("left" == whichPage || "both" == whichPage)
  308.                   {
  309.                      rect.width = origWidth / 2;
  310.                      pj.addPage(printedImg,rect,options);
  311.                      pagesToPrint++;
  312.                   }
  313.                   if("right" == whichPage || "both" == whichPage)
  314.                   {
  315.                      rect.x = rect.width = origWidth / 2;
  316.                      pj.addPage(printedImg,rect,options);
  317.                      pagesToPrint++;
  318.                   }
  319.                }
  320.                else
  321.                {
  322.                   pj.addPage(printedImg,rect,options);
  323.                }
  324.             }
  325.             catch(err:Error)
  326.             {
  327.                trace("printJob error " + err.message);
  328.             }
  329.             if(pagesToPrint > 0)
  330.             {
  331.                pj.send();
  332.             }
  333.             trace("currentWidth:",origWidth);
  334.             trace(printedImg.width,printedImg.height,printedImg.rotation);
  335.             printedImg.width = origWidth;
  336.             printedImg.height = origHeight;
  337.             printedImg.rotation = origRotation;
  338.             printedImg.scaleX = printedImg.scaleY = 1;
  339.             printedImg.validateNow();
  340.             trace("rescale");
  341.             trace("origWidth:",origWidth);
  342.             trace(printedImg.width,printedImg.height,printedImg.rotation);
  343.          }
  344.          else
  345.          {
  346.             trace("print job cancelled");
  347.          }
  348.       }
  349.    }
  350. }
  351.  
  352.